Page({

  data: {
    hotWords: [
      "机器学习", "Spring Cloud", "网站", "Redis", "Memcached", "Vue", "Spring MVC", "小程序", "Oracle", "Java"
    ],
    goodsName: '',
    result: []
  },

  onLoad() {
    setInterval(() => this.onRefresh(), 5000)
  },
  onReset() {
    this.setData({
      goodsName: '',
      result: []
    })
  },
  onSearch() {
    const { goodsName } = this.data
    if (!goodsName) {
      wx.showToast({
        title: '请输入关键字',
        icon: 'error'
      })
      return
    }
    wx.showLoading({
      title: '正在加载中'
    })
    wx.request({
      url: `${getApp<IAppOption>().globalData.baseURL}getGoodsList`,
      data: { goodsName },
      method: 'GET',
      success: ({ data }) => {
        console.log(data);
        // @ts-ignore
        const res = data.data
        this.setData({
          result: res
        })
        wx.hideLoading()
        if (!res.length)
          wx.showToast({
            title: '数据为空',
            icon: 'error'
          })
      }
    })
  },
  onRefresh() {
    const { hotWords } = this.data
    this.setData({
      hotWords: hotWords.sort(() => Math.random() - 0.5)
    })
  },
  onHot(e: WechatMiniprogram.ControlTap) {
    const goodsName = e.target.dataset.value
    this.setData({ goodsName })
    this.onSearch()
  }
})